<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 *
 * @package		Adminpro
 * @author		Tran Hoang Thien (thienhb12@gmail.com)
 * @copyright   PHP TEAM
 * @link		http://phpandmysql.net
 * @since		Version 1.0
 * @phone       0944418192
 *
 */
class Index extends MY_Controller {

	public function __construct(){
		parent:: __construct();
		$this->load->model('film_model');
		$this->load->model('category_model');
		$this->load->model('page_model');
		$this->load->model('config_model');
		$this->load->model('media_model');
		$this->load->model('muser');
	}

	public function index(){
		$this->cachesearch();
		if($this->session->userdata('session_user_id')){
			$id = $this->session->userdata('session_user_id');
				$this->settime_login($id);
		}
		$data                    = array();
		$data['config']          = $this->cache_site->get_info_cache($key = 'config',$time = 86400,$this->config_model->get_config_info());
		$data['title']           = $data['config']['cf_title'];
		$data['keywords']        = $data['config']['cf_seo'];
		$data['description']     = $data['config']['cf_meta'];
		$data['phone']           = $data['config']['cf_phone'];
		$data['email']           = $data['config']['cf_email'];
		$data['fanpage']		 = $data['config']['cf_fanpage'];                  	
		$data['result_cate']     = $this->cache_site->get_info_cache($key = 'cate',$time = 86400,$this->category_model->get_data_cate());
		$data['page']            = $this->cache_site->get_info_cache($key = 'page',$time = 86400,$this->page_model->get_list_page());
		$data['film_new']        = $this->cache_site->get_info_cache($key = 'film_new',$time = 1800,$this->film_model->film_new()) ;
		$data['full_episode']    = $this->cache_site->get_info_cache($key = 'full_episode',$time = 1800,$this->film_model->full_episode($limit = false,$start = false));
		$data['ratings_anime']   = $this->cache_site->get_info_cache($key = 'ratings_anime',$time = 1800,$this->film_model->ratings_anime());
		$data['ratings_cartoon'] = $this->cache_site->get_info_cache($key = 'ratings_cartoon',$time = 1800,$this->film_model->ratings_cartoon());
		$data['top_film']        = $this->cache_site->get_info_cache($key = 'top_film',$time = 1800,$this->film_model->top_film());
		$data['video']           = $this->cache_site->get_info_cache($key = 'video',$time = 1800,$this->media_model->get_data_media()); 
		$data['result_slider']	 = $this->cache_site->get_info_cache($key = 'result_slider',$time = 1800,$this->film_model->slider());
		$data['sidebar']         = 'sidebar';
		$data['template']        = 'home';
		$data['slider']          = 'slider/slider_film';
		$this->load->view("layout",$data);
	}
	
	public function cachesearch(){
		$search = PATH.'/assets/home/js/search.js';
		$expire = 86400;
		if(!file_exists($search)){
			$param           = array();
			$param['select'] = array('idphim','name','thumb','rewrite');
			$param['where']  = array('status' => 1);
			$arr             =  $this->film_model->get_list($param);
			if(!empty($arr)){
				foreach ($arr as $value) {
					$search_html[] = array('title' => $value['name'],'rewrite' => $value['rewrite'],'thumb' => $value['thumb'],'id'=> $value['idphim']);
				}
				$search_html = json_encode($search_html);
				$search_html = str_replace(array('[',']'),'',$search_html);
				END_CACHE('var ins_search = Array('.$search_html.')',$search);
			}
		}else{
			if(filemtime($search) < (time() - $expire)){
				$param           = array();
				$param['select'] = array('idphim','name','thumb','rewrite');
				$param['where']  = array('status' => 1);
				$arr             =  $this->film_model->get_list($param);
				if(!empty($arr)){
					foreach ($arr as $value) {
						$search_html[] = array('title' => $value['name'],'rewrite' => $value['rewrite'],'thumb' => $value['thumb'],'id'=> $value['idphim']);
					}
					$search_html = json_encode($search_html);
					$search_html = str_replace(array('[',']'),'',$search_html);
					END_CACHE('var ins_search = Array('.$search_html.')',$search);
				}
			}
		}
	}

	public function settime_login($id){
		$time_active = 1600;
		$time_login  = $this->session->userdata('session_logintime');
		if((time() - $time_active) > $time_login){
			$this->muser->save($id,array('status_login' => 0));
		}
	}
	
	
}

/* End of file index.php */
/* Location: ./application/modules/home/controllers/index.php */